	import java.util.ArrayList;
import java.util.HashMap;
	import java.util.List;

	import com.amazonaws.AmazonClientException;
	import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
	import com.amazonaws.auth.PropertiesCredentials;
	import com.amazonaws.services.simpledb.AmazonSimpleDB;
	import com.amazonaws.services.simpledb.AmazonSimpleDBClient;
	import com.amazonaws.services.simpledb.model.Attribute;
	import com.amazonaws.services.simpledb.model.BatchPutAttributesRequest;
	import com.amazonaws.services.simpledb.model.CreateDomainRequest;
	import com.amazonaws.services.simpledb.model.DeleteAttributesRequest;
	import com.amazonaws.services.simpledb.model.DeleteDomainRequest;
	import com.amazonaws.services.simpledb.model.Item;
	import com.amazonaws.services.simpledb.model.PutAttributesRequest;
	import com.amazonaws.services.simpledb.model.ReplaceableAttribute;
	import com.amazonaws.services.simpledb.model.ReplaceableItem;
import com.amazonaws.services.simpledb.model.SelectRequest;
import com.amazonaws.services.simpledb.model.PutAttributesRequest;;
public class DataBase {
	public static final String table="users24";
	AmazonSimpleDB sdb;
	public DataBase(AWSCredentials awsCredentials)
	{
		AWSCredentials aws = new BasicAWSCredentials("XXX", "X");
		sdb = new AmazonSimpleDBClient(aws);	
	}
	
	public void createTable(String myDomain)
	{
		 try {  
			 System.out.println("Creating domain called " + myDomain + ".\n");
		     sdb.createDomain(new CreateDomainRequest(myDomain));
	        } 
		 catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
     + "to Amazon SimpleDB, but was rejected with an error response for some reason.");
System.out.println("Error Message:    " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code:   " + ase.getErrorCode());
System.out.println("Error Type:       " + ase.getErrorType());
System.out.println("Request ID:       " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered "
     + "a serious internal problem while trying to communicate with SimpleDB, "
     + "such as not being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
}
		
	}
	
	public boolean Exist(String Tablename)
	{
     for (String domainName : sdb.listDomains().getDomainNames()) {
              if(Tablename.equals(domainName))
            	  return true;
          }
     return false;
	}
	
	public ArrayList<HashMap<String,String>> SelectQuery(String selectExpression)
	{
		ArrayList<HashMap<String,String>> arr=new ArrayList<HashMap<String,String>>(); 
		 HashMap<String,String> hm=new HashMap<String,String>();
		 SelectRequest selectRequest = new SelectRequest(selectExpression);
		 for (Item item : sdb.select(selectRequest).getItems()) {
             System.out.println("  Item");
             System.out.println("    Name: " + item.getName());
             for (Attribute attribute : item.getAttributes()) {
                 hm.put(attribute.getName(), attribute.getValue());
                 System.out.println("        Name:  " + attribute.getName());
                 System.out.println("        Value: " + attribute.getValue());
             }
             arr.add(hm);
		 }
         return arr;
	}
	
	public void InsertQuery(HashMap<String,String> hm)
	{
		 List<ReplaceableItem> sampleData = new ArrayList<ReplaceableItem>();

	        sampleData.add(new ReplaceableItem(hm.get("AWSAccessKeyId")).withAttributes(
	                new ReplaceableAttribute("ip", hm.get("ip"), true),
	                new ReplaceableAttribute("AWSAccessKeyId", hm.get("AWSAccessKeyId"), true),
	                new ReplaceableAttribute("volumnId", hm.get("volumnId"), true),
	                new ReplaceableAttribute("AMIid", hm.get("AMIid"), true)));
	        sdb.batchPutAttributes(new BatchPutAttributesRequest(table, sampleData));

	}
	
	public void UpdateQuery(String attribute, String value, String keyid)
	{
		 List<ReplaceableAttribute> replaceableAttributes = new ArrayList<ReplaceableAttribute>();
         replaceableAttributes.add(new ReplaceableAttribute(attribute, value, true));
         sdb.putAttributes(new PutAttributesRequest(table, keyid, replaceableAttributes));
	}
}
